from struct import *
from xml.dom import minidom
class MemImage:
       def __init__(self,filename,uuid,network,image=""):
            self.uuid = uuid;
            self.network = network;
            self.image = image;
            self.filename = filename;
            self.head = "";
            self.version = 0;
            self.was_running = 0;
            self.unused=[]
            self.xml_len = 0;
            self.tail = "";
            self.headinfoList=[]
       def read(self):
            try:
                fd = open(self.filename)
                headinfo = fd.read(92)
                self.headinfoList = unpack("<16slll16l",headinfo)
                self.xml_len = self.headinfoList[2]
                self.xml_len = self.xml_len - 1;
                self.xml = fd.read(self.xml_len)
                pos = self.xml.find("</domain>")
                self.tail = fd.read();
                self.tail = self.xml[pos+9:]+self.tail;
                self.xml = self.xml[0:pos+9];
                fd.close();
                return 0;
            except Exception,e:
                raise Exception,"Failed to read memory image file --- "+str(e)
                return -1;
            
       def do_modify(self):
            try:
                xmldoc = minidom.parseString(self.xml)
                xmldoc.getElementsByTagName("name")[0].firstChild.data=self.uuid
                xmldoc.getElementsByTagName("uuid")[0].firstChild.data=self.uuid
            
                if self.image!="":
                   disk = xmldoc.getElementsByTagName("disk")[0]
                   source = disk.getElementsByTagName("source")[0]
                   source.attributes["file"].value = self.image
                if self.network!=None:
                   interfaces = xmldoc.getElementsByTagName("interface")
                   i = 0;
                   for interface in interfaces:
                       netsource = interface.getElementsByTagName("source")[0]
                       netsource.attributes["network"].value = self.network[i]
                       i=i+1;       
                   
                self.xml = xmldoc.toxml();          
                pos = self.xml.find("<domain")  
                self.xml = self.xml[pos:]
                return 0;
            except Exception,e:
                raise Exception,"failed to modify memory image---"+str(e)
                return -1;

       def do_write(self):
            fd = open(self.filename,"wb")
            head = pack("<16slll16l",*self.headinfoList)
            fd.write(head)
            fd.write(self.xml)
            fd.write(self.tail)
            fd.close();
       def modify(self):
            try:
                if self.read()!=0:
                    return -1;
                if self.do_modify()!=0:
                    return -1;
                self.do_write();
                return 0;
            except Exception,e:
                raise Exception,"modify memory image failed---"+str(e)
                return -1;
            
if __name__=="__main__":
     mem = MemImage("/root/temp","69b27060-679e-11de-8c38-00188b1b8d0d",None,"/var/lib/ivic/vmc/nfs/192.168.4.170/_var_lib_ivic_www_vstore_nfscow/69b27060-679e-11de-8c38-00188b1b8d0d.img")
     mem.modify();
      
